const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');

module.exports = function (config, env) {
  config.module.loaders[0].exclude.push(/.ejs$/);    // 注 1
  if (env === 'production' || env === 'demo') {
    config.output.filename = '[hash].js';
    config.output.chunkFilename = '[hash].async.js';
    config.plugins[3] = new ExtractTextPlugin('[hash].css');    // 注 2
    config.plugins.push(
      new HtmlWebpackPlugin({
        template: 'ejs!src/index.ejs',    // 注 3
        inject: false,
        minify: { collapseWhitespace: true },
        production: true,
      }),
      new WebpackMd5Hash(),
    );
    // config.output.publicPath = process.env.PUBLIC_PATH || '/';
    // config.plugins.push(
    //   new webpack.DefinePlugin({
    //     API_URL: JSON.stringify(process.env.API_URL),
    //   }),
    // );
  } else {
    config.plugins.push(
      new HtmlWebpackPlugin({
        template: 'ejs!src/index.ejs',
        inject: true,
      }),
    );
  }
  return config;
};
